<?php

namespace App\Models;

use App\Models\customer\OrderRuleModel;
use App\Models\customer\ResourceModel;
use App\Models\finance\InvoiceModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CustomerModel extends BaseModel
{
    use HasFactory, SoftDeletes;

    protected $table = 't_customer';

    protected $primaryKey = 'id';

    public $timestamps = true;

    const CREATED_AT = 'created_at';
    
    const UPDATED_AT = null;

    protected $guarded = [];

    protected $hidden = [
        'deleted_at',
    ];

    //账号状态
    const NORMAL_ACCOUNT = 1;  //正常
    const BLOCK_ACCOUNT = 2;  //禁用
    const ACCOUNT_STATUS = [
        self::NORMAL_ACCOUNT => '正常',
        self::BLOCK_ACCOUNT => '禁用',
    ];

    //结算类型
    const TICK_SETTLE = 1;  //票结
    const SEVEN_SETTLE = 2;  //7日结算
    const FIFTEEN_SETTLE = 3;  //15天结算
    const THIRTY_SETTLE = 4;  //30天结算
    const SETTLE_TYPES = [
        self::TICK_SETTLE => '票结',
        self::SEVEN_SETTLE => '7日结算',
        self::FIFTEEN_SETTLE => '15日结算',
        self::THIRTY_SETTLE => '30日结算',
    ];

    //收取提单费
    const COLLECT_BILL_FEE = 1;  //收取
    const NOT_COLLECT_BILL_FEE = 2;  //不收取
    
    //收取分单费
    const COLLECT_SUB_BILL_FEE = 1;  //收取
    const NOT_COLLECT_SUB_BILL_FEE = 2;  //不收取

    //合同是否顺延
    const CONTRACT_DELAY = 1;  //顺延
    const NOT_CONTRACT_DELAY = 2;  //不顺延

    //审核状态 1待审核 2通过 3驳回
    const PENDING_REVIEW = 1;  //待审核
    const REVIEW_PASSED = 2;  //通过
    const REVIEW_REJECTED = 3;  //驳回
    const REVIEW_STATUS = [
        self::PENDING_REVIEW => '待审核',
        self::REVIEW_PASSED => '通过',
        self::REVIEW_REJECTED => '驳回',
    ];


    //创建人
    public function create_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'created_admin_id');
    }

    //客户类型
    public function customer_type(): BelongsTo {
        return $this->belongsTo(CustomerTypeModel::class, 'customer_type_id');
    }

    //开户受理人
    public function handle_staff(): BelongsTo {
        return $this->belongsTo(StaffModel::class, 'handle_staff_id');
    }

    //收费设置
    public function charges(): HasMany {
        return $this->hasMany(CustomerChargeModel::class, 'customer_id');
    }

    //开票设置
    public function invoice(): HasOne {
        return $this->hasOne(CustomerInvoiceModel::class, 'customer_id');
    }

    //大客户入仓单号规则
    public function order_rule(): HasOne
    {
        return $this->hasOne(OrderRuleModel::class, 'customer_id');
    }

    //大客户资源代码
    public function resource(): HasOne
    {
        return $this->hasOne(ResourceModel::class, 'customer_id')->where('resource_type', ResourceModel::RESOURCE_TYPE_EML);
    }

    //省
    public function province(): BelongsTo
    {
        return $this->belongsTo(CountryModel::class, 'province_id');
    }

    //市
    public function city(): BelongsTo
    {
        return $this->belongsTo(CountryModel::class, 'city_id');
    }

    public function invoice2(): MorphOne
    {
        return $this->MorphOne(InvoiceModel::class, 'invoiceable');
    }

}
